package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.OrderSettingDao;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;

    @Override
    public Result order(Map map) throws Exception {
        //检查当前日期是否进行了预约设置
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findCountByOrderDate2(date);
        if (orderSetting == null) {
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        //检查预约日期是否预约已满
        int number = orderSetting.getNumber();//可预约人数
        int reservations = orderSetting.getReservations();//已预约人数
        if (reservations >= number) {
            //预约已满，不能预约
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //检查当前用户是否为会员，根据手机号判断
        String telephone = (String) map.get("telephone");
        Member member = memberDao.findByTelephone(telephone);
        if (member != null) {
            Integer memberId = member.getId();
            int setmealId = Integer.parseInt((String) map.get("setmealId"));
            Order order = new Order(memberId, date, null, null, setmealId);
            List<Order> list = orderDao.findByCondition(order);
            if (list != null && list.size() > 0) {
                //已经完成了预约，不能重复预约
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        }
        //可以预约，设置预约人数加一
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.editReservationsByOrderDate(orderSetting);
        if (member == null) {
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            memberDao.add(member);
        }
        Order order = new Order(member.getId(),
                date,
                (String) map.get("orderType"),
                Order.ORDERSTATUS_NO,
                Integer.parseInt((String) map.get("setmealId")));
        orderDao.add(order);
        return new Result(true, MessageConstant.ORDER_SUCCESS, order.getId());
    }

    @Override
    public Map findById(Integer id) throws Exception {
        Map map = orderDao.findById4Detail(id);
        if(map != null){
            Date orderDate = (Date)map.get("orderDate");
            map.put("orderDate",DateUtils.parseDate2String(orderDate));
        }
        return map;
    }

    /**
     * 根据日期范围查询每日预约数，每日到诊数
     * @param map
     * @return
     */
    @Override
    public Map<String,Object> findAandVCount(Map map) throws Exception {

        //查询日期范围 日期和每日预约数
        List<Map<String, Object>> orderlist = orderDao.findOrderDateByCondition(map);
        List<Long> integers2Order = new ArrayList<>(); //保存每日的预约数组
        if(orderlist != null && orderlist.size()>0){
            for (Map<String, Object> objectMap : orderlist) {
                Long count = (Long) objectMap.get("count");
                integers2Order.add(count);
            }
        }
        Map<String,Object> orderMap = new HashMap<>();
        orderMap.put("name","每日预约数");
        orderMap.put("type","line");
       // orderMap.put("stack","总量");
        orderMap.put("data",integers2Order);


        //查询日期范围 日期和每日到诊数
        List<Map<String, Object>> visitlist = orderDao.findVisitDateByCondition(map);
        List<Long> integers2Visit = new ArrayList<>();
        List<String> dateList = new ArrayList<>();
        if(visitlist != null && visitlist.size()>0){
            for (Map<String, Object> objectMap : visitlist) {
                Long count = (Long) objectMap.get("count"); //获取到诊数
                String string = DateUtils.parseDate2String((Date) objectMap.get("orderdate"));
                ;  //获取日期
                integers2Visit.add(count);
                dateList.add(string);
            }
        }
        Map<String,Object> visitMap = new HashMap<>();
        visitMap.put("name","每日到诊数");
        visitMap.put("type","line");
        //visitMap.put("stack","总量");
        visitMap.put("data",integers2Visit);

        List<Map<String,Object>> dataList = new ArrayList<>();
        dataList.add(orderMap);
        dataList.add(visitMap);

        Map<String,Object> datamap = new HashMap<>();
        datamap.put("AandVData",dataList); //添加具体数据
        datamap.put("dateList",dateList);  //添加日期列表
        return datamap;
    }
}
